package com.fand.platform.authority.controller;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.Validator;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fand.context.mapper.JsonMapper;
import com.fand.dto.PaginationDto;
import com.fand.platform.authority.dto.NavigationDto;
import com.fand.platform.authority.dto.NavigationLaDto;
import com.fand.platform.authority.service.AuthorityService;
import com.fand.platform.authority.validator.NavigationLaValidator;
import com.fand.platform.authority.validator.NavigationValidator;
import com.fand.platform.authority.vo.NavigationVo;
import com.google.common.collect.Maps;

/**
 * 导航
 * 
 * @author yaoHT
 */
@Controller
public class NavigationController {
	@Qualifier(AuthorityService.BEAN_ID)
	@Autowired(required = false)
	private AuthorityService authorityService;

	@Qualifier(NavigationValidator.BEAN_ID)
	@Autowired(required = false)
	private Validator navigationValidator;

	@Qualifier(NavigationLaValidator.BEAN_ID)
	@Autowired(required = false)
	private Validator navigationLaValidator;

	@RequestMapping(value = { "/admin/authority/navigation/navigation/delete.json" }, method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> adminNavigationDelete(final NavigationVo navigationVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.authorityService.getNavigationService().deleteNavigationBy2(navigationVo.getNavigation());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return jsonMap;
	}

	@RequestMapping(value = { "/admin/authority/navigation/navigation/edit.html" }, method = RequestMethod.GET)
	public String adminNavigationEdit(final NavigationVo navigationVo, final Model model) {
		final NavigationDto navigation = this.authorityService.getNavigationService().getNavigationBy2(
				navigationVo.getNavigation());

		model.addAttribute("navigation", navigation);

		return "/admin/authority/navigation/navigation/edit";
	}

	@RequestMapping(value = { "/admin/authority/navigation/navigation.html",
			"/admin/authority/navigation/navigation/index.html" })
	public String adminNavigationIndex() {
		return "/admin/authority/navigation/navigation/index";
	}

	@RequestMapping(value = { "/admin/authority/navigation/navigation/indexPagination.json" })
	public @ResponseBody
	Map<String, Object> adminNavigationIndexPagination(final NavigationVo navigationVo) {
		final PaginationDto<Map<String, Object>> navigationPagination = this.authorityService.getNavigationService()
				.paginationNavigationBy1ForMapList(navigationVo.getNavigation());

		final Map<String, Object> navigationPaginationMap = Maps.newHashMap();

		navigationPaginationMap.put("total", navigationPagination.getTotalSize());
		navigationPaginationMap.put("rows", navigationPagination.getDataList());

		return navigationPaginationMap;
	}

	@RequestMapping(value = { "/admin/authority/navigation/navigationLa/delete.json" }, method = RequestMethod.POST)
	public @ResponseBody
	Map<String, Object> adminNavigationLaDelete(final NavigationVo navigationVo) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.authorityService.getNavigationService().deleteNavigationLaBy2(navigationVo.getNavigationLa());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return jsonMap;
	}

	@RequestMapping(value = { "/admin/authority/navigation/navigationLa/edit.html" }, method = RequestMethod.GET)
	public String adminNavigationLaEdit(final NavigationVo navigationVo, final Model model) {
		final NavigationLaDto navigationLa = this.authorityService.getNavigationService().getNavigationLaBy2(
				navigationVo.getNavigationLa());

		model.addAttribute("navigationLa", navigationLa);

		return "/admin/authority/navigation/navigationLa/edit";
	}

	@RequestMapping(value = { "/admin/authority/navigation/navigationLa.html",
			"/admin/authority/navigation/navigationLa/index.html" })
	public String adminNavigationLaIndex() {
		return "/admin/authority/navigation/navigationLa/index";
	}

	@RequestMapping(value = { "/admin/authority/navigation/navigationLa/indexPagination.json" })
	public @ResponseBody
	Map<String, Object> adminNavigationLaIndexPagination(final NavigationVo navigationVo) {
		final PaginationDto<Map<String, Object>> navigationLaPagination = this.authorityService
				.getNavigationService().paginationNavigationLaBy1ForMapList(navigationVo.getNavigationLa());

		final Map<String, Object> navigationLaPaginationMap = Maps.newHashMap();

		navigationLaPaginationMap.put("total", navigationLaPagination.getTotalSize());
		navigationLaPaginationMap.put("rows", navigationLaPagination.getDataList());

		return navigationLaPaginationMap;
	}

	@RequestMapping(value = { "/admin/authority/navigation/navigationLa/new.html" }, method = RequestMethod.GET)
	public String adminNavigationLaNew(final NavigationVo navigationVo, final Model model) {
		model.addAttribute("navigationLa", navigationVo.getNavigationLa());

		return "/admin/authority/navigation/navigationLa/new";
	}

	@RequestMapping(value = { "/admin/authority/navigation/navigationLa/save.txt" }, method = RequestMethod.POST)
	public @ResponseBody
	String adminNavigationLaSave(final NavigationVo navigationVo, BindingResult bindingResult) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.navigationLaValidator.validate(navigationVo.getNavigationLa(), bindingResult);

			if (bindingResult.hasErrors()) {
				String message = StringUtils.EMPTY;

				for (final ObjectError objectError : bindingResult.getAllErrors()) {
					message += objectError.getDefaultMessage();
				}

				throw new Exception(message);
			}

			this.authorityService.getNavigationService().saveNavigationLaBy1ForNavigationLa(
					navigationVo.getNavigationLa());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return JsonMapper.nonEmptyMapper().toJson(jsonMap);
	}

	@RequestMapping(value = { "/admin/authority/navigation/navigationLa/validationBy1.json" })
	public @ResponseBody
	Boolean adminNavigationLaValidationBy1(final NavigationVo navigationVo) {
		try {
			final int count = this.authorityService.getNavigationService().readNavigationLaBy1ForInteger(
					navigationVo.getNavigationLa());

			return (count < 1);
		} catch (final Exception e) {
		}

		return false;
	}

	@RequestMapping(value = { "/admin/authority/navigation/navigationLa/view.html" }, method = RequestMethod.GET)
	public String adminNavigationLaView(final NavigationVo navigationVo, final Model model) {
		final NavigationLaDto navigationLa = this.authorityService.getNavigationService().getNavigationLaBy2(
				navigationVo.getNavigationLa());

		model.addAttribute("navigationLa", navigationLa);

		return "/admin/authority/navigation/navigationLa/view";
	}

	@RequestMapping(value = { "/admin/authority/navigation/navigation/new.html" }, method = RequestMethod.GET)
	public String adminNavigationNew(final NavigationVo navigationVo, final Model model) {
		model.addAttribute("navigation", navigationVo.getNavigation());

		return "/admin/authority/navigation/navigation/new";
	}

	@RequestMapping(value = { "/admin/authority/navigation/navigation/save.txt" }, method = RequestMethod.POST)
	public @ResponseBody
	String adminNavigationSave(final NavigationVo navigationVo, BindingResult bindingResult) {
		final Map<String, Object> jsonMap = Maps.newHashMap();

		try {
			this.navigationValidator.validate(navigationVo.getNavigation(), bindingResult);

			if (bindingResult.hasErrors()) {
				String message = StringUtils.EMPTY;

				for (final ObjectError objectError : bindingResult.getAllErrors()) {
					message += objectError.getDefaultMessage();
				}

				throw new Exception(message);
			}

			this.authorityService.getNavigationService().saveNavigationByIdOrCodeForNavigation(
					navigationVo.getNavigation());
		} catch (final Exception e) {
			jsonMap.put("message", e.getMessage());
		}

		return JsonMapper.nonEmptyMapper().toJson(jsonMap);
	}

	@RequestMapping(value = { "/admin/authority/navigation/navigation/validationBy1.json" })
	public @ResponseBody
	Boolean adminNavigationValidationBy1(final NavigationVo navigationVo) {
		try {
			final int count = this.authorityService.getNavigationService().readNavigationBy1ForInteger(
					navigationVo.getNavigation());

			return (count < 1);
		} catch (final Exception e) {
		}

		return false;
	}

	@RequestMapping(value = { "/admin/authority/navigation/navigation/view.html" }, method = RequestMethod.GET)
	public String adminNavigationView(final NavigationVo navigationVo, final Model model) {
		final NavigationDto navigation = this.authorityService.getNavigationService().getNavigationBy2(
				navigationVo.getNavigation());

		model.addAttribute("navigation", navigation);

		return "/admin/authority/navigation/navigation/view";
	}
}
